package com.cuz.daileetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Day14_3 {

    /**
     * @author cuzz
     * @version 1.0
     * @description: 双指针
     * @date 22:43 2021/10/2
     **/
    static class S2 {
        public List<List<Integer>> fourSum(int[] nums, int target) {
            List<List<Integer>> res = new ArrayList<>();
            if (nums == null || nums.length < 4) {
                return res;
            }
            Arrays.sort(nums);
            for (int firstIndex = 0; firstIndex < nums.length - 3; firstIndex++) {
                //当前数和前面的一样 那么跳过 去重
                if (firstIndex - 1 >= 0 && nums[firstIndex] == nums[firstIndex - 1]) {
                    continue;
                }
                //最前面的四个数大于target 已经找不到这么四个数了
                if (nums[firstIndex] + nums[firstIndex + 1] + nums[firstIndex + 2] + nums[firstIndex + 3] > target) {
                    break;
                }
                //最后面三个数加起来都小于target 这个数属于已经需要remake了
                if (nums[firstIndex] + nums[nums.length - 1] + nums[nums.length - 2] + nums[nums.length - 3] < target) {
                    continue;
                }

                for (int secondIndex = firstIndex + 1; secondIndex < nums.length - 2; secondIndex++) {
                    if (secondIndex >firstIndex+1&& nums[secondIndex] == nums[secondIndex - 1]) {
                        continue;
                    }
                    //最前面的四个数大于target 已经找不到这么四个数了
                    if (nums[firstIndex] + nums[secondIndex] + nums[secondIndex + 1] + nums[secondIndex + 2] > target) {
                        break;
                    }
                    //最后面三个数加起来都小于target 这个数属于已经需要remake了
                    if (nums[firstIndex] + nums[nums.length - 1] + nums[nums.length - 2] + nums[secondIndex] < target) {
                        continue;
                    }
                    int leftIndex = secondIndex + 1;
                    int rightIndex = nums.length - 1;
                    //变成两数之和
                    while (leftIndex < rightIndex) {
                        int curSum = nums[firstIndex] + nums[secondIndex]
                                + nums[rightIndex] + nums[leftIndex];
                        if (curSum > target) {
                            rightIndex--;
                        } else if (curSum < target) {
                            leftIndex++;
                        } else {
                            res.add(Arrays.asList(nums[firstIndex], nums[secondIndex]
                                    , nums[leftIndex], nums[rightIndex]));
                            while (leftIndex < rightIndex && nums[leftIndex] == nums[leftIndex + 1]) {
                                leftIndex++;
                            }
                            leftIndex++;
                            while (leftIndex < rightIndex && nums[rightIndex] == nums[rightIndex - 1]) {
                                rightIndex--;
                            }
                            rightIndex--;
                        }
                    }
                }

            }
            return res;
        }
    }

    class s11 {

        public List<List<Integer>> fourSum(int[] nums, int target) {
            List<List<Integer>> res = new ArrayList<List<Integer>>();
            if (nums == null || nums.length < 4) {
                return res;
            }
            Arrays.sort(nums);
            int length = nums.length;
            for (int firstIndex = 0; firstIndex < length - 3; firstIndex++) {
                if (firstIndex > 0 && nums[firstIndex] == nums[firstIndex - 1]) {
                    continue;
                }
                if (nums[firstIndex] + nums[firstIndex + 1] + nums[firstIndex + 2] + nums[firstIndex + 3] > target) {
                    break;
                }
                if (nums[firstIndex] + nums[length - 3] + nums[length - 2] + nums[length - 1] < target) {
                    continue;
                }
                for (int secondIndex = firstIndex + 1; secondIndex < length - 2; secondIndex++) {
                    if (secondIndex > firstIndex + 1 && nums[secondIndex] == nums[secondIndex - 1]) {
                        continue;
                    }
                    if (nums[firstIndex] + nums[secondIndex] + nums[secondIndex + 1] + nums[secondIndex + 2] > target) {
                        break;
                    }
                    if (nums[firstIndex] + nums[secondIndex] + nums[length - 2] + nums[length - 1] < target) {
                        continue;
                    }
                    int left = secondIndex + 1, right = length - 1;
                    while (left < right) {
                        int sum = nums[firstIndex] + nums[secondIndex] + nums[left] + nums[right];
                        if (sum == target) {
                            res.add(Arrays.asList(nums[firstIndex], nums[secondIndex], nums[left], nums[right]));
                            while (left < right && nums[left] == nums[left + 1]) {
                                left++;
                            }
                            left++;
                            while (left < right && nums[right] == nums[right - 1]) {
                                right--;
                            }
                            right--;
                        } else if (sum < target) {
                            left++;
                        } else {
                            right--;
                        }
                    }
                }
            }
            return res;
        }
    }

}
